import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Properties;

import br.ufmg.vod.wbr.algorithims.components.WeaklyConnectedComponentBFS;
import br.ufmg.vod.wbr.algorithims.components.WeaklyConnectedComponentDFS;
import br.ufmg.vod.wbr.graphs.DirectedToUndirected;
import br.ufmg.vod.wbr.graphs.FastDirectedGraph;
import br.ufmg.vod.wbr.graphs.FastGraph;
import br.ufmg.vod.wbr.graphs.IDXGraphLoader;


public class MyWCC {

	/**
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {

		Properties props = new Properties(); 
		props.load(ClassLoader.getSystemResourceAsStream("collection.properties"));
		String inLinkIdx = props.getProperty("collection.path")+props.getProperty("inlinks.idx");
		String inLink = props.getProperty("collection.path")+props.getProperty("inlinks");
		String outLinkIdx = props.getProperty("collection.path")+props.getProperty("outlinks.idx");
		String outLink = props.getProperty("collection.path")+props.getProperty("outlinks");
		boolean sample = Boolean.valueOf(props.getProperty("sample"));
		int sampleSize = Integer.valueOf(props.getProperty("sample.size"));
		System.out.println(props);
		
		
		long start = System.currentTimeMillis();
		FastDirectedGraph graph = new IDXGraphLoader(inLinkIdx, inLink, outLinkIdx, outLink, sample, sampleSize).loadGraph();
		FastGraph undirectedGraph = new DirectedToUndirected(graph).loadGraph();
		System.err.printf("GraphContructor: %.3f seconds.\n",(System.currentTimeMillis()-start)/1000f);
		
		
		WeaklyConnectedComponentBFS wcc = new WeaklyConnectedComponentBFS(undirectedGraph);
//		WeaklyConnectedComponentDFS wcc = new WeaklyConnectedComponentDFS(undirectedGraph);
		wcc.run();
		System.err.printf("SSC: %.3f seconds.\n",(System.currentTimeMillis()-start)/1000f);
		System.err.println(wcc.getGiantComponent().length);
		System.err.println(wcc.getComponents().size());
		

		
		ArrayList<int[]> distribution = new ArrayList<int[]>(wcc.getComponents());
		Collections.sort(distribution, new LengthComparator());
		for (int[] component : distribution) {
			System.out.print(component.length);
			for (int vertex : component) {
				System.out.print(" "+vertex);
			}
			System.out.println();
		}
	}
	
	static class LengthComparator implements Comparator<int[]>{
		@Override
		public int compare(int[] o1, int[] o2) {
			return o1.length-o2.length;
		}
	}

}
